package com.woniu.system.service.imp;

import com.woniu.components.DAO.DAOHelper;
import com.woniu.components.DAO.DisPage;
import com.woniu.components.DAO.IEntity;
import com.woniu.system.pojo.SysCompany;
import com.woniu.system.service.ICompanyManager;
import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xujin
 * Date: 2010-2-15
 * Time: 14:42:57
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CompanyManager implements ICompanyManager {
    @Resource
    DAOHelper woniuDAOHelper;



    public void find() {

        Object obj = null;
        try {
            obj = woniuDAOHelper.findByPk(SysCompany.class, 2L);

        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        if (obj != null) {
            SysCompany company = (SysCompany) obj;

        }
    }

    public IEntity getCompany(Long id) throws Exception {
        return woniuDAOHelper.findByPk(SysCompany.class, id);
    }

    public List findAll() {
        DisPage disPage = new DisPage();
        disPage.setStartNum(0);
        disPage.setPageSize(5);
        List<Order> ordreList = new ArrayList<Order>();
        ordreList.add(Order.asc("SState"));
        ordreList.add(Order.desc("NId"));

        List list = woniuDAOHelper.findList(SysCompany.class, new ArrayList<Criterion>(), disPage, ordreList);
//     List list =   woniuDAOHelper.findAll(SysCompany.class,null,order);
        System.out.println(disPage.getTotalRows());
        System.out.println(disPage.getCurrentPage());
        System.out.println(disPage.getTotalPages());
        for (Object obj : list) {
            SysCompany company = (SysCompany) obj;
        }


        return list;
    }

    public SysCompany save(Map map) throws Exception {
        SysCompany company = new SysCompany();
        BeanUtils.copyProperties(company, map);
        woniuDAOHelper.persist(company);
        return company;
    }

    public void update(Map map) {

//            SysCompany company = (SysCompany) woniuDAOHelper.findByPk(SysCompany.class,2L);

        SysCompany company = new SysCompany();
        company.setNId(2L);
        company.setSName("xujin0022237");
        company.setSState("1");
        try {
            woniuDAOHelper.updateEntity(company);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void delete(Long id) throws Exception {

        woniuDAOHelper.delete(SysCompany.class, id);
//
//        List<SysDictionaryId> ids = new ArrayList<SysDictionaryId>();
//        SysDictionaryId sysDictionaryId = new SysDictionaryId();
//        sysDictionaryId.setNTypeId(1L);
//        sysDictionaryId.setSCode("0");
//        ids.add(sysDictionaryId);
//        SysDictionaryId sysDictionaryId1 = new SysDictionaryId();
//        sysDictionaryId1.setNTypeId(3L);
//        sysDictionaryId1.setSCode("1");
//        ids.add(sysDictionaryId1);
//        SysDictionaryId sysDictionaryId2 = new SysDictionaryId();
//        sysDictionaryId2.setNTypeId(3L);
//        sysDictionaryId2.setSCode("0");
//        ids.add(sysDictionaryId2);
//        SysDictionaryId sysDictionaryId3 = new SysDictionaryId();
//        sysDictionaryId3.setNTypeId(4L);
//        sysDictionaryId3.setSCode("1");
//        ids.add(sysDictionaryId3);
//        try {
//            woniuDAOHelper.deleteList(SysDictionary.class, ids);
//        } catch (Exception e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }

    }
     public DAOHelper getDAOHelper() {
        return woniuDAOHelper;
    }

}
